﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  WorkStationUpdateOrOutput, PrintEquipmentServiceProxy, PrintEquipmentDto, UpdatePrintEquipment, CreatePrintEquipment, CompanyDto, OrganizationUnitServiceProxy, ComboboxItemDto, UpdateEquipment, EquipmentDto, EquipmentServiceProxy, CreateEquipment } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './equipment.component.html',
    styleUrls: ['./equipment.component.less'],
    animations: [appModuleAnimation()]
})
export class EquipmentComponent extends AppComponentBase implements OnInit  {

    @ViewChild('dt') dataTable: Table;

    displayDialog: boolean;
    selectedEquipment: UpdateEquipment;    
    newEquipment: boolean;    
    equipments: EquipmentDto[] = [];

    equipmentTypeSelected: '';
    equipmentTypeCombobox: ComboboxItemDto[]= [];

    myCompanyCode = '';

    constructor(
        injector: Injector,
        private _equipmentServiceProxy: EquipmentServiceProxy,
        private _organizationUnitServiceProxy : OrganizationUnitServiceProxy
        ) 
        {
            super(injector);            
        }

    
    
    cols: any[];
    ngOnInit(): void {
        this.cols = [
            { field: 'id', header: '编号' },
            { field: 'displayName', header: '名称' },
            { field: 'equipmentType', header: '类型' },
            { field: 'location', header: '位置' }
        ];
        this.initEquipmentTypeCombobox();

        this.initMyCompanyCode();
        this.getAllEquipment();
        
    }

    initMyCompanyCode()
    {
        this._organizationUnitServiceProxy.getMyCompanyCode().subscribe(result=>{
            this.myCompanyCode = result;

        });
    }

    initEquipmentTypeCombobox()
    {
        this._equipmentServiceProxy.getComboboxItemForEquipmentType()
        .subscribe(result => {
            result.forEach(c => {
                let companyItem = new ComboboxItemDto();
                companyItem.value = c.value;
                companyItem.displayText = c.displayText;
                this.equipmentTypeCombobox.push(companyItem);
                console.log(companyItem.displayText );
            }); 
        });
    }

    getAllEquipment(){
        this._equipmentServiceProxy.getAll('','','',0,200)
        .subscribe(result => {
            this.equipments = result.items;
          });       

    }

    showDialogToAdd() {
        this.newEquipment = true;
        this.selectedEquipment = new UpdateEquipment();
        this.displayDialog = true;
    }


    save() {        

        this.selectedEquipment.companyCode = this.myCompanyCode;
         if(this.newEquipment){            
            this._equipmentServiceProxy.create(this.cloneUpdateToCreate(this.selectedEquipment))
            .subscribe(result => {
                this.getAllEquipment();
            });
        }else
        {
            this._equipmentServiceProxy.update(this.selectedEquipment)
            .subscribe(() => {
                this.getAllEquipment();
            });

        }
       
        this.displayDialog = false;   
        
    }

    delete() {
        this._equipmentServiceProxy.delete(this.selectedEquipment.id)
        .subscribe(result => {
            this.getAllEquipment();
          });
        
        this.selectedEquipment = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newEquipment = false;
        this.selectedEquipment = this.cloneEquipment(event.data);
        this.displayDialog = true;
    }

    cloneEquipment(c: EquipmentDto): UpdateEquipment {
        let station = new UpdateEquipment();
        for (let prop in c) {
            station[prop] = c[prop];
        }
        return station;
    }

    cloneUpdateToCreate(c: UpdateEquipment): CreateEquipment {
        let station = new CreateEquipment();
        for (let prop in c) {
            if(prop!='id')
                station[prop] = c[prop];
        }
        return station;
    }
      
    print(qrcode:string,stationName:string) {
        
    }
}
